package com.kabigon.tiny.dataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * master 数据源配置
 * @author xu.biao
 * @version v1.0
 * @Package com.kabigon.tiny.dataSource
 * @data 2022/12/17 1:09
 */
//@Configuration
//@MapperScan(basePackages = DataSourceMasterConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class DataSourceMasterConfig {
    // master dao所在的包
    public static final String PACKAGE = "com.kabigon.tiny.mapper.master";
    // 配置文件所在目录
    private static final String MAPPER_LOCATION = "classpath*:mapper/master/**/*.xml";

    //@Bean("masterDataSource")
    //@Primary // 默认访问
    //@ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 创建session
     */
    //@Primary // 默认访问
    //@Bean("masterSqlSessionFactory")
    public SqlSessionFactoryBean masterSqlSessionFactory() {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(masterDataSource());
        //Resource resource = new PathMatchingResourcePatternResolver().getResource(MAPPER_LOCATION);
        //sqlSessionFactoryBean.setMapperLocations(resource);
        return sqlSessionFactoryBean;
    }

    /**
     * 数据源事务管理器
     */
    //@Primary // 默认访问
    //@Bean("masterDataSourceTransactionManager")
    public DataSourceTransactionManager masterDataSourceTransactionManager() {
        return new DataSourceTransactionManager(masterDataSource());
    }
}
